﻿using EmbeddedSharp.Core;
using Zove.Runtime.Core.Expression;

namespace Zove.Runtime.Core.Zoom
{
    /// <summary>
    /// Parser factory
    /// </summary>
    public class ZoomExpressionParserFactory : IExpressionParserFactory {
        /// <summary>
        /// Function manager
        /// </summary>
        public IFunctionManager FunctionManager { get; set; }

        /// <summary>
        /// Language nature
        /// </summary>
        public ILanguageNature LanguageNature { get; private set; }

        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="languageNature">language nature</param>
        public ZoomExpressionParserFactory(ILanguageNature languageNature)
        {
            LanguageNature = languageNature;
        }

        #region IExpressionParserFactory Members

        /// <summary>
        /// Create an expression parser
        /// </summary>
        /// <param name="expr">the expression</param>
        /// <returns>the expression parser</returns>
        public ExpressionParser CreateParser(string expr) {
            return CreateParser(expr, null);
        }

        /// <summary>
        /// Create an expression parser
        /// </summary>
        /// <param name="expr">the expression</param>
        /// <param name="context">the context</param>
        /// <returns>the expression parser</returns>
        public ExpressionParser CreateParser(string expr, object context) {
            var runtimeModel = context as RuntimeModel;

            var expressionParser 
                = new ZoomExpressionParser(expr, runtimeModel)
                      {
                          ConstantManager = DefaultDependencyManager.Instance.ConstantManager,
                          FunctionManager = LanguageNature.FunctionManager,
                          ExpressionParserFactory = this
                      };

            return expressionParser;
        }

        #endregion
    }
}